<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/28 0028
 * Time: 17:08
 */
const FUNC_ONE_ARGS = 1; //函数参数为1个
const FUNC_TWO_ARGS = 2; //函数参数为2个
function session()
{
    static $ci;
    if(empty($ci)){
        $ci = &get_instance();
    }
    $func_args_num = func_num_args();
    $ci->load->library("session");
    if($func_args_num==FUNC_ONE_ARGS)
    {
        $func_keys = func_get_args();
        $session_key = reset($func_keys);
        return $ci->session->userdata($session_key);
    }else if($func_args_num==FUNC_TWO_ARGS)
    {
        list($session_key,$session_val) = func_get_args();
        $ci->session->set_userdata($session_key,$session_val);
    }
}
//查询ip地址的物理位置
function getAddressByIp($ip)
{
    $get_data = @file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip");
    $data = json_decode($get_data,true);

    if(empty($data['code']))
    {
        $country = $data['data']['country'];
        $city = $data['data']['region'];
        $isp = $data['data']['isp'];
        $return_data = compact('country','city','isp');
        return $return_data;
    }
    else{
       return false;
    }
}

//查询主机端口是否开放
function checkPortStatusByIP($server,$port,$timeout=2)
{
    $url = preg_match("/^(http:\/\/|https:\/\/)/",$server); //是否为域名
    if($url)
    {
        $server = gethostbyname($url); //通过域名获取到ip
    }
    if($server && $port && $timeout){
        $verbinding = @fsockopen("$server", $port, $errno, $errstr, $timeout);
    }
    return $verbinding?true:false;
}